package com.android.qjpmc.widget.manager;

import android.content.Intent;
import android.view.View;

import androidx.fragment.app.FragmentActivity;

import com.android.qjpmc.application_module.pop.ListCheckPopupWindow;
import com.android.qjpmc.bean.ListCheckPopBean;
import com.android.qjpmc.widget.retrofithelper.Tls12SocketFactory;
import com.android.qjpmc.widget.retrofithelper.http.Url;
import com.android.qjpmc.widget.utils.ToastUtils;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2018/7/10.
 */

public class HttpsManager {

    public static void setCertificates(OkHttpClient.Builder clientBuilder, InputStream... certificates) {
        try {
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            keyStore.load(null);
            int index = 0;
            for (InputStream certificate : certificates) {
                String certificateAlias = Integer.toString(index++);
                keyStore.setCertificateEntry(certificateAlias, certificateFactory
                        .generateCertificate(certificate));
                try {
                    if (certificate != null)
                        certificate.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
                    TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(keyStore);
            TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
            if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
                throw new IllegalStateException("Unexpected default trust managers:"
                        + Arrays.toString(trustManagers));
            }
            X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
            SSLSocketFactory socketFactory = new Tls12SocketFactory(sslContext.getSocketFactory());
            clientBuilder.sslSocketFactory(socketFactory, trustManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void onSwitchApi(FragmentActivity activity) {
        ListCheckPopBean selectApiType = new ListCheckPopBean();

        selectApiType.id = Url.BASE_URL;
        if (Url.BASE_URL.contains("59.36.165.191")) {
            selectApiType.name = "测试环境";
        } else if (Url.BASE_URL.contains("47.103.204.127")) {
            selectApiType.name = "正式环境";
        }else{
            ToastUtils.showShort("出现错误,请联系管理员");
            return;
        }
        ListCheckPopupWindow queryApiPopupWindow = new ListCheckPopupWindow(activity);
        List<ListCheckPopBean> popListData = new ArrayList<>();
        popListData.add(new ListCheckPopBean("http://59.36.165.191:8085/", "测试环境", selectApiType.name.equals("测试环境")));
        popListData.add(new ListCheckPopBean("http://47.103.204.127:3200/api/", "正式环境", selectApiType.name.equals("正式环境")));
        queryApiPopupWindow.initListData(popListData);
        queryApiPopupWindow.setListCheckPopupWindowListener(new ListCheckPopupWindow.ListCheckPopupWindowListener() {
            @Override
            public void onPopChooseResult(List<ListCheckPopBean> popListData) {
                ListCheckPopBean popListDatumTemp = null;
                for (ListCheckPopBean popListDatum : popListData) {
                    if (popListDatum.isOpen) {
                        popListDatumTemp = popListDatum;
                        break;
                    }
                }
                if (popListDatumTemp == null) {
                    return;
                }
                SPManager.BaseUrlConfigData.setBaseUrl(popListDatumTemp.id);
                final Intent intent = activity.getPackageManager().getLaunchIntentForPackage(activity.getPackageName());
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                activity.startActivity(intent);

                //杀掉以前进程
                android.os.Process.killProcess(android.os.Process.myPid());

            }
        });

        View view = activity.getWindow().getDecorView();
        queryApiPopupWindow.showPopupWindowAtLocation(view);


    }


}
